<template>
    <el-container class="base_express_container">
        <el-main>
            <el-card class="box-card">
                <headerTemplate slot="header" ref="header"
                                :table-ref="tableOptions.ref"
                                :toolbar_buttons="headerOption.toolbar_buttons"
                                :toolbar_filter="headerOption.toolbar_filter"
                                :searchForm="searchForm"
                                @event="handleHeaderEvent" />
                <vxeTableTemplate :data="dataList" :options.sync="tableOptions"
                                  @event="handleTableEvent" />
            </el-card>
        </el-main>

        <component-dialog dialog-drag :options="componentDialogOption" @event="handleDialogEvent" />
        <component-dialog dialog-drag :options="batchComponentDialogOption" />
    </el-container>
</template>

<script>
import baseExpressApi from "@common/api/base/baseExpressApi";
import ExpressForm from "../dialog/express/ExpressForm";
import FreightSetting from "../dialog/express/FreightSetting";
import BatchForm from "../dialog/express/BatchForm";
import provide from "@common/mixins/provide";

export default {
    extends: provide,
    name: "Express",
    data() {
        let $this = this;
        return {
            searchForm: {},
            headerOption: {
                toolbar_buttons: [
                    {
                        id: "refresh",
                        icon: "el-icon-refresh-right",
                        show: true,
                        method: () => {
                            $this.getList();
                        },
                    },
                    {
                        id: "add",
                        icon: "fa fa-plus",
                        type: 'primary',
                        show: true,
                        permission: 'save_base_exprss_org',
                        method: () => {
                            $this.openDialog(null, 'add');
                        },
                    },
                    {
                        id: "edit",
                        icon: "fa fa-pencil",
                        type: 'success',
                        show: true,
                        permission: 'save_base_exprss_org',
                        method: () => {
                            $this.openDialog(null, 'edit');
                        },
                    },
                    {
                        id: "delete",
                        icon: "fa fa-trash-o",
                        type: 'danger',
                        show: true,
                        permission: 'del_base_exprss_org',
                        method: () => {
                            $this.delExpress(null, 'batch');
                        },
                    },
                ],
            },
            dataList: [],
            selectionList: [],
            tableOptions: {
                ref: 'BaseExpressTable',
                container: '.base_express_container',
                multiSelect: true,
                columns: [
                    {
                        label: '编码',
                        prop: 'code',
                        align: 'center',
                    },
                    {
                        label: '物流公司',
                        prop: 'name',
                        align: 'center',
                    },
                    {
                        label: '排序',
                        prop: 'sn',
                        align: 'center',
                    }
                ],
                operates: {
                    width: 200,
                    list: [ {
                        id: 'info',
                        icon: 'fa fa-pencil',
                        type: 'success',
                        size: 'mini',
                        show: true,
                        tooltip: {
                            content: '编辑',
                        },
                        permission: 'save_base_exprss_org',
                        method: (index, row) => {
                            $this.openDialog(row, 'edit');
                        }
                    }, {
                        id: 'info',
                        icon: 'el-icon-setting',
                        type: 'primary',
                        size: 'mini',
                        show: true,
                        tooltip: {
                            content: '运费设置',
                        },
                        permission: 'express_freight_setting',
                        method: (index, row) => {
                            $this.openSettingDialog(row);
                        }
                    }, {
                        id: 'info',
                        icon: 'el-icon-setting',
                        size: 'mini',
                        show: true,
                        tooltip: {
                            content: '运费设置详情',
                        },
                        method: (index, row) => {
                            $this.openSettingDialog(row, true);
                        }
                    }, {
                        id: 'info',
                        icon: 'fa fa-trash-o',
                        type: 'danger',
                        size: 'mini',
                        show: true,
                        tooltip: {
                            content: '删除',
                        },
                        permission: 'del_base_exprss_org',
                        method: (index, row) => {
                            $this.delExpress(row)
                        }
                    } ],
                },
                pagination: {
                    pageIndex: 1,
                }
            },
            batchComponentDialogOption: {
                visible: false,
            },
        }
    },
    mounted() {
        this.getList();
    },
    methods: {
        handleHeaderEvent(event) {
            let $this = this;
            $this.searchForm = $.extend({}, $this.searchForm, event.searchForm);
            if (event.type === "search") {
                $this.getList(true);
            } else if (event.type === "refresh") {
                $this.getList(true);
            }
        },
        getList(search) {
            let $this = this;
            $this.dataList = [];
            if (search) {
                $this.tableOptions = $this.resetTablePagination($this.tableOptions);
            }
            let queryData = $this.getQueryParams($this.tableOptions, $this.searchForm);
            queryData.with_item = '1';
            baseExpressApi.findBaseExpressOrgs(queryData, result => {
                $this.dataList = result.data || [];
                $this.tableOptions = $this.setTableTotal($this.tableOptions, result);
            });
        },
        handleTableEvent(event) {
            if (event.type === 'page') {
                this.getList();
                return;
            }
            if (event.type === 'selectionChange') {
                this.selectionList = event.value;
            }
        },
        openDialog(row, type) {
            const _this = this;
            let title = type === 'add' ? '创建' : '编辑';
            if (!row) {
                if (type === 'add') {
                    row = {};
                }
                if (type === 'edit') {
                    if (!_this.checkSelection(_this.selectionList)) {
                        return;
                    }
                    row = $.extend({}, _this.selectionList[0]);
                }
            }
            _this.openComponentDialog({
                width: '600px',
                title,
                component: ExpressForm,
                propsData: {
                    row,
                },
                callback() {
                    _this.getList();
                },
            });
        },
        openSettingDialog(row, detail) {
            const _this = this;
            let options = {
                width: '1000px',
                title: '运费设置',
                component: FreightSetting,
                propsData: {
                    row,
                    detail,
                },
                callback() {
                    _this.getList();
                },
            };
            if (detail) {
                options.btns = [];
            }
            _this.openComponentDialog(options);
        },
        //删除
        delExpress(row, type) {
            let $this = this;
            let ids = [];
            if (type === 'batch') {
                if ($this.selectionList.length === 0) {
                    $this.$message.error("请选中一条数据");
                    return;
                } else {
                    ids = $this.selectionList.map(item => item.id);
                }
            } else {
                ids.push(row.id);
            }
            $this.confirmDel(ids);
        },
        confirmDel(ids) {
            let $this = this;
            $this.$confirm("是否继续删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                baseExpressApi.batchDelExpress({ ids }, () => {
                    $this.getList();
                    $this.$message.success("删除成功");
                    $this.$bus.$emit('refreshExpressList');
                })
            }).catch(() => {
            });
        },
        handleDialogEvent(event) {
            const _this = this;
            if (event.type === 'openBatchDialog') {
                _this.openComponentDialog({
                    width: '500px',
                    title: '批量设置费率表',
                    component: BatchForm,
                    propsData: {
                        rateList: event.data.rateList,
                    },
                    btns: [
                        {
                            type: 'primary',
                            text: '确定',
                            loading: true,
                            method(option, { component }) {
                                component && typeof component.confirm === 'function' && component.confirm(event._component).then(valid => {
                                    if (valid) {
                                        option.visible = false;
                                    }
                                })
                            },
                        }
                    ],
                }, 'batchComponentDialogOption');
            }
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.base_express_container {
    height: 100%;

    .buttons {
        margin: 0 0 10px 0;
    }
}
</style>
